package merisis.silo;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

import merisis.util.EncodeUtil;

public class Silo {

	private File siloHome;

	public final int SILO_FILE_SIZE = 1 << 23;

	public SiloName makesense(String identifier) {

		return null;
	}

	public SiloTuple getTuple(SiloName name) {
		return null;
	}

	public SiloName setTuple(SiloTuple tuple) {

		return null;
	}

	public SiloContentChannel getContentChannel(SiloName name) {
		return null;
	}

	/**
	 * or tuple.set( 10, SiloThis.newEntity());
	 * 
	 * @param name
	 */
	public void erase(SiloName name) {

	}
	
	// Trunk: 2**23, Slot 2**10, Block 2**7
	//

	public void openTrunk(long trunkno) throws IOException {

		String fname;
		RandomAccessFile file;

		fname = EncodeUtil.encodeBase32(trunkno, 8) + ".silo";
		file = new RandomAccessFile(new File(siloHome, fname), "rw");

		FileChannel channel = file.getChannel();

		if (file.length() == 0) {
			ByteBuffer filehead = ByteBuffer.allocate(32);
			// //byte[] head = new byte[byteLengthOfTrunkHead];
			// head[23] = (byte) 0xFF; // the end of head
			// filehead.put(head);
			filehead.putLong(1);
			filehead.flip();
			channel.write(filehead);
			channel.force(true);
		}
	}
}
